package com.swt.shoppingmall.shoppingcart.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseArray;

import com.alibaba.fastjson.JSON;
import com.swt.shoppingmall.app.MyApplication;
import com.swt.shoppingmall.home.bean.GoodsBean;
import com.swt.shoppingmall.utils.CacheUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车
 * Created by swt on 2016/12/26.
 */

public class CartStorage {
    public static final String JSON_CART = "json_cart";
    private Context mContext;
    private static CartStorage instance;
    private final SparseArray<GoodsBean> sparseArray;

    private CartStorage(Context context) {
        this.mContext = context;
        //把之前的数据读取出来
        sparseArray = new SparseArray<>(100);

        listToSparseArray();
    }

    /**
     * 从本地读取的数据加入到SparseArray中
     */
    private void listToSparseArray() {
        List<GoodsBean> goodsBeanList = getAllData();
        //把goodsBeanList列表数据转换成sparseArray
        for (int i = 0; i < goodsBeanList.size(); i++) {
            GoodsBean goodsBean = goodsBeanList.get(i);
            sparseArray.put(Integer.parseInt(goodsBean.getProduct_id()), goodsBean);
        }
    }

    public static CartStorage getInstance() {
        if (null == instance) {
            instance = new CartStorage(MyApplication.mContext);
        }

        return instance;
    }

    /**
     * 获取本地所有的数据
     *
     * @return
     */
    public List<GoodsBean> getAllData() {
        List<GoodsBean> goodsBeanList = new ArrayList<>();
        //1.从本地获取
        String json = CacheUtils.getString(mContext, JSON_CART);
        //2.使用json转换成列表
        if (!TextUtils.isEmpty(json)) {
            //直接把String类型转成list
            goodsBeanList = JSON.parseArray(json, GoodsBean.class);
        }
        return goodsBeanList;
    }

    public void addDate(GoodsBean goodsBean) {
        //添加到内存中SparseArray
        //如果当前数据已经存在，只需要修改数量number
        GoodsBean tempData = sparseArray.get(Integer.parseInt(goodsBean.getProduct_id()));
        if (tempData != null) {
            tempData.setNumber(tempData.getNumber() + 1);
        } else {
            tempData = goodsBean;
            tempData.setNumber(1);
        }
        sparseArray.put(Integer.parseInt(goodsBean.getProduct_id()), tempData);
        //同步到本地sp
        saveLocal();
    }

    /**
     * 保存数据到本地
     */
    private void saveLocal() {
        //1.将sparseArray转成list集合
        List<GoodsBean> goodsBeanList = sparseArrayToList();
        //2.把列表转成string类型
        String json = JSON.toJSONString(goodsBeanList);
        //3.保存String类型数据
        CacheUtils.saveString(mContext, JSON_CART, json);
    }

    private List<GoodsBean> sparseArrayToList() {
        List<GoodsBean> goodsBeanList = new ArrayList<>();
        for (int i = 0; i < sparseArray.size(); i++) {
            GoodsBean goodsBean = sparseArray.valueAt(i);
            goodsBeanList.add(goodsBean);
        }
        return goodsBeanList;
    }

    /**
     * 删除数据
     *
     * @param goodsBean
     */
    public void deleteData(GoodsBean goodsBean) {
        //1.内存中的数据
        sparseArray.delete(Integer.parseInt(goodsBean.getProduct_id()));
        //2.把内存中的数据保存到本地sp中
        saveLocal();
    }

    /**
     * 修改数据
     *
     * @param goodsBean
     */
    public void updateData(GoodsBean goodsBean) {
        //1.内存中的数据
        sparseArray.put(Integer.parseInt(goodsBean.getProduct_id()), goodsBean);
        //2.把内存中的数据保存到本地sp中
        saveLocal();
    }


}
